<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\base\Exception;
use yii\db\Query;
class CorrectCardRecharge extends ActiveRecord
{
    public $iccid_list;
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['iccid_list'],'required'],
            
        ];
    }

    public function attributeLabels()
    {
        return [
            'iccid_list' => 'ICCID列表',
            
        ];
    }
    /**
     * 检查iccid
     */
    public static function checkIccid($iccid_list='',$isp_id,$uid,$agent='',$package_id='',$term_length='',$term_length_unit=''){
        if(!empty($iccid_list)){
            $iccidList = preg_split('/\s/',$iccid_list, -1, PREG_SPLIT_NO_EMPTY);

            foreach ($iccidList as $key=>$item){
                $card = Card::find()->asArray()->where(['iccid' => $item])->one();
                if (empty($card)) {
                    //Card::addError('iccid_list', 'ICCID【' . $iccid . '】不是有效的卡。');
                    return ['code'=>1001,'msg'=>'ICCID【' . $item . '】不是有效的卡。'];
                }

                // if (!empty($card['uid']) || !empty($card['agent_id'])) {
                //     return ['code'=>1001,'msg'=>'ICCID【' . $item . '】已经有分配的用户了。'];
                // }

                $cardList[] = $card;
                $cardData[$key]['id'] = $card['id'];
                $cardData[$key]['iccid'] = $item;
                $cardData[$key]['uid'] = $uid;
                $cardData[$key]['agent_id'] = $agent;
                $cardData[$key]['package_id'] = $package_id;
                $cardData[$key]['dispatch_time'] = time();
                $cardData[$key]['term_length'] = $card['term_length']+$term_length;
                $cardData[$key]['term_length_unit'] = $term_length_unit;
                $cardData[$key]['dgcs'] = $card['dgcs'] + 1;
            }
            foreach($cardData as $ckey=>$cval){
                $cvaid= $cval["id"];
                $carddata['iccid']=$cval['iccid'];
                $carddata['uid']=$cval['uid'];
                $carddata['agent_id']=$cval['agent_id'];
                $carddata['package_id']=$cval['package_id'];
                $carddata['dispatch_time']=time();
                $carddata['term_length']=$cval['term_length'];
                $carddata['term_length_unit']=$cval['term_length_unit'];
                $carddata['dgcs'] = $cval['dgcs'];
                $carddata['status']=4;
                $res = (new Query())->createCommand()
                    ->update('card',$carddata,['id'=>$cvaid])
                    ->execute();
                if(!$res){
                    return ['code'=>1001,'msg'=>'ICCID【' . $cval['iccid'] . '】更新失败。'];
                    break;
                }
            }
            return ['code'=>1,'msg'=>''];

        }
    }
    

    /**
     * 关联套餐
     * @return \yii\db\ActiveQuery
     */
    public function getPackage()
    {
        return $this->hasOne(Package::class, ['id' => 'package_id']);
    }

    /**
     * 关联代理商
     * @return \yii\db\ActiveQuery
     */
    public function getAgent()
    {
        return $this->hasOne(Agent::class, ['id' => 'agent_id']);
    }
}
